package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
public class EmpServiceImpl implements EmpService {

    //依赖注入
    @Autowired
    private EmpMapper empMapper;

    /**
     * 分页查询员工信息
     *
     * @param page
     * @param pageSize
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @return PageBean
     */
    @Override
    public PageBean limit(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
     /*   //1.查询指定页码数据列表
        List<Emp> list = empMapper.limit((page-1)*pageSize+1,pageSize);
        //2.查询数据条数
        long total = empMapper.count();
        //3.将数据封装到PageBean
        PageBean pageBean = new PageBean(total,list);*/

        //分页插件
        PageHelper.startPage(page, pageSize);
        Page<Emp> startPage = empMapper.limit(name,gender,begin,end);
        PageBean pageBean = new PageBean(startPage.getTotal(),startPage.getResult());
        return pageBean;
    }

    /**
     * 根据id删除员工
     *
     * @param ids
     */
    @Override
    public void deleteById(Integer[] ids) {
        //调用mapper层
        empMapper.deleteById(ids);

    }

    /**
     * 添加员工信息
     *
     * @param emp
     */
    @Override
    public void add(Emp emp) {
        //补全字段信息
        emp.setPassword("123456");
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        //调用mapper层
        empMapper.insert(emp);
    }

    /**
     * 根据id查询员工信息
     *
     * @param id
     * @return
     */
    @Override
    public Emp getById(Integer id) {
        //调用mapper层
        Emp emp = empMapper.getById(id);
        return emp;
    }

    /**
     * 根据id修改员工信息
     *
     * @param emp
     */
    @Override
    public void update(Emp emp) {
        //补全字段信息
        emp.setUpdateTime(LocalDateTime.now());
        //调用mapper层
        empMapper.update(emp);
    }

    /**
     * 用户登录
     *
     * @param emp
     * @return
     */
    @Override
    public Emp login(Emp emp) {
        //调用mapper层
        Emp e = empMapper.findByUsernameAndPassword(emp.getUsername(),emp.getPassword());

        return e;
    }

}
